package com.tjetc.dao.impl;

import com.tjetc.dao.MedicineDao;
import com.tjetc.domain.Medicine;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class MedicineDaoImpl implements MedicineDao {
    @Override
    public int getCount(String name) throws SQLException {
        return ((Long)Db.runner0().query("select count(id) from medicine where name like ? ",
                new ScalarHandler<>(),"%"+name+"%")).intValue();
    }

    @Override
    public List<Medicine> list(String name, PageInfo<Medicine> pageInfo) throws SQLException {
        return Db.runner0().query("select * from medicine where name like ? limit ?,?",
                new BeanListHandler<>(Medicine.class),
                "%"+name+"%",pageInfo.getStartRecord(),pageInfo.getPageSize());
    }

    @Override
    public void add(Medicine medicine) throws SQLException {
        Db.runner().update(Db.getConnection(),"insert into medicine(name,cid,unit,restamount,warnamount,price,address,remarks) values(?,?,?,?,?,?,?,?)",
                medicine.getName(),medicine.getCid(),medicine.getUnit(),medicine.getRestamount(),medicine.getWarnamount(),
                medicine.getPrice(),medicine.getAddress(),medicine.getRemarks());
    }

    @Override
    public int findMaxId() throws SQLException {
        return Db.runner().query(Db.getConnection(),"select max(id) from medicine",
                new ScalarHandler<>());
    }

    @Override
    public void update(Medicine medicine) throws SQLException {
        Db.runner().update(Db.getConnection(),"update medicine set name=?,cid=?,unit=?,restamount=?,warnamount=?,price=?,address=?,remarks=? where id=?",
                medicine.getName(),medicine.getCid(),medicine.getUnit(),medicine.getRestamount(),medicine.getWarnamount(),
                medicine.getPrice(),medicine.getAddress(),medicine.getRemarks(),medicine.getId());
    }

    @Override
    public void cutDown(int id, int num) throws SQLException {
        Db.runner().update(Db.getConnection(),"update medicine set restamount = restamount - ? where id = ?",
                num,id);
    }

    @Override
    public void delById(int id) throws SQLException {
        Db.runner().update(Db.getConnection(),"delete from medicine where id=?",
                id);
    }

    @Override
    public List<Medicine> listAll() throws SQLException {
        return Db.runner0().query("select * from medicine",new BeanListHandler<>(Medicine.class));
    }

    @Override
    public Medicine getById(int id) throws SQLException {
        return Db.runner0().query("select * from medicine where id = ?",
                new BeanHandler<>(Medicine.class),id);
    }

    @Override
    public Medicine findByName(String name) throws SQLException {
        return Db.runner0().query("select * from medicine where name=?",new BeanHandler<>(Medicine.class),name);
    }
}
